package com.cyf.activemq.service.impl;

import com.cyf.activemq.service.SendMsgService;
import com.cyf.activemq.service.bean.QueueList;
import com.cyf.util.ObjectMapperUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

/**
 * Created by cage on 2017/3/15.
 */
@Service
public class SendMsgServiceImpl implements SendMsgService {

    private static final Logger logger = LoggerFactory.getLogger(SendMsgServiceImpl.class);

    @Autowired
    private JmsTemplate jmsTemplate;

    @Override
    public void sendQueueMessageText(String message) {
        logger.info("activemq发送消息：{}", message);
        jmsTemplate.convertAndSend(QueueList.QUEUE_SEND, message);
    }

    @Override
    public boolean send(Object object) {
        try {
            String message = ObjectMapperUtils.getObjectMapper().writeValueAsString(object);
            sendQueueMessageText(message);
        } catch (JsonProcessingException e) {
            logger.error("{}", e);
            return false;
        }
        return true;
    }

    @Override
    public void sendQueueMessageTextPay(String message) {
        logger.info("activemq发送支付消息：{}", message);
        jmsTemplate.convertAndSend(QueueList.QUEUE_RECEIVE_PAY, message);
    }
}
